package com.sunyard.utils.manage;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

public class FastdfsUtils {
    // 声明跟踪器客户端对象
    private static TrackerClient trackerClient = null;
    // 声明存储器客户端对象
    private static StorageClient1 storageClient1 = null;
    // 声明跟踪器服务对象
    private static TrackerServer trackerServer = null;
    // 声明存储器服务对象
    private static StorageServer storageServer = null;

    // 文件上传
    public static String testUpload(String local, String fastdfsConfigLocal) {
        try {
            // 初始化配置文件
            //ClientGlobal.init("fdfs_client.conf");
            // ClientGlobal.init("application-dev.properties");
            //ClassPathResource cpr = new ClassPathResource("application-dev.properties");
            //ClientGlobal.init(cpr.getClassLoader().getResource("application-dev.properties").getPath());
            //System.out.println("输出配置文件地址"+fastdfsConfigLocal);
            ClientGlobal.init(fastdfsConfigLocal);
            // 创建跟踪器客户端对象
            trackerClient = new TrackerClient();
            // 获取跟踪器连接
            trackerServer = trackerClient.getConnection();
            // 获取存储器客户端对象
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            // 上传文件，返回文件标识
            String index = storageClient1.upload_file1(local, null, null);
            // 查看标识
            System.out.println(index);
            return index;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (MyException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 文件删除
    public static String testDelete(String local, String fastdfsConfigLocal) {
        try {
            //ClientGlobal.init("application-dev.properties");
            //ClientGlobal.init("fdfs_client.conf");
            //ClassPathResource cpr = new ClassPathResource("application-dev.properties");
            //ClientGlobal.init(cpr.getClassLoader().getResource("application-dev.properties").getPath());
            ClientGlobal.init(fastdfsConfigLocal);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            // 根据文件标识删除文件，返回0则删除成功
            int i = storageClient1.delete_file1(local);
            if (i == 0) {
                System.out.println("服务器文件删除成功");
                return "删除成功";
            } else {
                System.out.println("服务器文件删除成功");
                return "删除失败";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (MyException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 文件下载
    public static byte[] testDownload(String local, String fastdfsConfigLocal) {
        try {
            //ClientGlobal.init("fdfs_client.conf");
            //ClientGlobal.init("application-dev.properties");
            //ClassPathResource cpr = new ClassPathResource("application-dev.properties");
            //ClientGlobal.init(cpr.getClassLoader().getResource("application-dev.properties").getPath());
            ClientGlobal.init(fastdfsConfigLocal);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            // 根据文件标识下载文件
            byte[] by = storageClient1.download_file1(local);
            return by;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (MyException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 文件信息
    public void testGetFileInfo(String local, String fastdfsConfigLocal) {
        try {
            //ClientGlobal.init("fdfs_client.conf");
            //ClientGlobal.init("application-dev.properties");
            //ClassPathResource cpr = new ClassPathResource("application-dev.properties");
            //ClientGlobal.init(cpr.getClassLoader().getResource("application-dev.properties").getPath());
            ClientGlobal.init(fastdfsConfigLocal);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            // 根据文件标识获取文件信息
            FileInfo fileInfo = storageClient1.get_file_info1("group1/M00/00/00/wKhphVry2QmAXgH2AANfM1yHJic724.txt");
            // 文件IP地址
            System.out.println(fileInfo.getSourceIpAddr());
            // 文件大小
            System.out.println(fileInfo.getFileSize());
            // 创建时间
            System.out.println(fileInfo.getCreateTimestamp());
            // 错误校验码
            System.out.println(fileInfo.getCrc32());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }


}
